package com.moriarty.socketim.entity;

import com.google.gson.Gson;
import com.thoughtworks.xstream.XStream;

/**
 * Created by Moriarty on 16/11/16.
 */

public class FormatObject {

    //将本类转化为xml
    public String toXml() {

        XStream xstream = new XStream();
        //修正xml的根节点内的信息（只要QQMessage）
        xstream.alias(this.getClass().getSimpleName(), this.getClass());
        String xml = xstream.toXML(this);

        return xml;
    }

    //将xml转化为本类
    public Object fromXml(String xml) {
        XStream xStream = new XStream();
        xStream.alias(this.getClass().getSimpleName(), this.getClass());
        Object obj = xStream.fromXML(xml);

        return obj;
    }

    //将本类转化为json
    public String toJson() {
        Gson gson = new Gson();
        String json = gson.toJson(this);
        return json;
    }

    //将json转化为本类
    public Object fromJson(String json) {
        Gson gson = new Gson();
        Object obj = gson.fromJson(json, this.getClass());

        return obj;
    }
}
